1635E - Cars - CodeForces Solution


2-sat constructive algorithms dfs and similar dsu graphs greedy sortings *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define mp make_pair
#define F first
#define S second

int n, m;
vector<pair<pii, bool>> qs;  // true = irrelevant; false = destined
vector<vector<int>> adj;     // to check if bipartite
vector<int> comp;            // 0 = unvisited; 1 = left; 2 = right
vector<vector<int>> dir;     // for topsort
vector<int> indeg;           // for topsort
vector<int> pos;             // answer

bool dfs(int p) {
  for (int q : adj[p]) {
    if (comp[q] == 0) {
      comp[q] = 3 - comp[p];
      if (!dfs(q)) return false;
    }
    if (comp[q] == comp[p]) {
      return false;
    }
  }
  return true;
}

void compute() {
  for (auto q : qs) {
    adj[q.F.F].push_back(q.F.S);
    adj[q.F.S].push_back(q.F.F);
  }

  comp.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    if (comp[i] != 0) continue;

    comp[i] = 1;
    if (!dfs(i)) {
      cout << "NO\n";
      return;
    }
  }

  dir.resize(n + 1);
  indeg.resize(n + 1);
  // it is now known that the graph is bipartite
  for (auto q : qs) {
    if (comp[q.F.S] == 1) swap(q.F.F, q.F.S);
    if (q.S) {
      dir[q.F.F].push_back(q.F.S);
      indeg[q.F.S]++;
    } else {
      dir[q.F.S].push_back(q.F.F);
      indeg[q.F.F]++;
    }
  }

  // for (int i = 1; i <= n; i++) {
  //   cout << i << " : ";
  //   for (int q : dir[i]) cout << q << " ";
  //   cout << "\n";
  // }

  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indeg[i] == 0) {
      q.push(i);
    }
  }
  vector<int> order;
  while (!q.empty()) {
    int curr = q.front();
    // cout << curr << "\n";
    q.pop();
    order.push_back(curr);
    for (int next : dir[curr]) {
      indeg[next]--;
      if (indeg[next] == 0) {
        q.push(next);
      }
    }
  }

  if ((int)order.size() != n) {  // loop exists, bad
    cout << "NO\n";
    return;
  }

  pos.resize(n + 1);
  for (int i = 0; i < n; i++) pos[order[i]] = i;

  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << (comp[i] == 1 ? "L" : "R") << " " << pos[i] << "\n";
  }
}

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  cin >> n >> m;
  adj.resize(n + 1);

  while (m--) {
    int t, a, b;
    cin >> t >> a >> b;
    qs.push_back(mp(mp(a, b), t == 1));
  }

  compute();

  return 0;
}


Comments

Submit
0 Comments
More Questions

MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image